<?php
App::uses('AppController', 'Controller');
/**
 * Sessaos Controller
 *
 * @property Sessao $Sessao
 */
class UserpagesController extends AppController {

	var $uses = 'Userpage';
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('menu', 'menu_mais', 'view', 'home');
	}
	
	public function index() {
		$this->Userpage->recursive = 0;
		$this->set('pages', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($caminho = null) {
		if (!$this->Userpage->findByCaminho($caminho)) {
			throw new NotFoundException(__('Página inválida'));
		}
		$options = array('conditions' => array('Userpage.caminho' => $caminho));
		$page = $this->Userpage->find('first', $options);
		$this->set('page', $page);
		$this->set('title_for_layout', $page['Userpage']['titulo']);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Userpage->create();
			$data = $this->request->data;
			$data['Userpage']['caminho'] = $this->Userpage->toAscii($data['Userpage']['titulo']);
			if ($this->Userpage->save($data)) {
				$this->Session->setFlash(__('Infopágina salva com sucesso!'), 'flash/bootstrap', array('class'=>'alert alert-success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Infopágina não salva'), 'flash/bootstrap', array('class'=>'alert alert-error'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Userpage->exists($id)) {
			throw new NotFoundException(__('Página inválida'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$data = $this->request->data;
			$data['Userpage']['caminho'] = $this->Userpage->toAscii($data['Userpage']['titulo']);
			if ($this->Userpage->save($data)) {
				$this->Session->setFlash(__('Infopágina salva com sucesso!'), 'flash/bootstrap', array('class'=>'alert alert-success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Infopágina não salva'), 'flash/bootstrap', array('class'=>'alert alert-error'));
			}
		} else {
			$options = array('conditions' => array('Userpage.' . $this->Userpage->primaryKey => $id));
			$this->request->data = $this->Userpage->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Userpage->id = $id;
		if (!$this->Userpage->exists()) {
			throw new NotFoundException(__('Página inválida'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Userpage->delete()) {
			$this->Session->setFlash(__('Página excluída'), 'flash/bootstrap', array('class'=>'alert alert-success'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Página não foi excluída'), 'flash/bootstrap', array('class'=>'alert alert-error'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function menu(){
		$this->layout = false;
		$this->autoRender = false;
		return $this->Userpage->find('all', array('conditions'=>array('menu'=>1), 'order'=>'ordem_menu ASC'));
	}
	
	public function menu_mais(){
		$this->layout = false;
		$this->autoRender = false;
		return $this->Userpage->find('all', array('conditions'=>array('menu_mais'=>1), 'order'=>'ordem_menu_mais ASC'));
	}
	
	public function set_home($id){
		$home_atual = $this->Userpage->findByHome(1);
		$this->Userpage->updateAll(array('home' => NULL));
		if(!isset($home_atual['Userpage']) || $id != $home_atual['Userpage']['id']){
			$this->Userpage->id = $id;
			$this->Userpage->saveField('home', 1);
		}
		$this->redirect('index');
	}
	
	public function home(){
		$page = $this->Userpage->findByHome(1);
		if(!$page){
			$this->redirect('/');
		}
		$this->set('page', $page);
		$this->set('title_for_layout', $page['Userpage']['titulo']);
		$this->render('view');
	}
}
